<?php
include 'sysco.function.php';


$con = mysql_connect($HOST, $USER, $PASS);
mysql_select_db($DB);

$menuProdCountArr = array('lp000051383792' => 506, 'lp000051396857' => 292, 'lpLaVigaRwc' => 121);
$menuProdCount = $menuProdCountArr[$MERCHANT];

$trendByCount = array();
$gain = array();
$gain2 = array();
// by TOTAL_SALE_COUNT
$last30d = trends($MERCHANT, 'TOTAL_SALE_COUNT');

if ($month == 1) {
  $_SESSION['from'] = '2012-12-01 03:00:00';
  $_SESSION['to'] = '2013-01-01 02:59:00';
} else if ($month == 2) {
  $_SESSION['from'] = '2013-01-01 03:00:00';
  $_SESSION['to'] = '2013-02-01 02:59:00';
}
$last60d30d = trends($MERCHANT, 'TOTAL_SALE_COUNT');
$prodIds = array();
echo $MERCHANT."\n";

foreach ($last60d30d as $id => $item) {
  $prodIds[] = '';
  if (isset($last30d[$id])) {
    if ($last30d[$id]['TOTAL'] < 100 || $last30d[$id]['TOTAL_SALE_COUNT'] < 5) {
      // continue;
    }
    $trendByCount[$id]['NAME'] = $last30d[$id]['NAME'];
    $trendByCount[$id]['CODE'] = $last30d[$id]['CODE'];
    $trendByCount[$id]['PRICESELL'] = $last30d[$id]['PRICESELL'];
    $trendByCount[$id]['TOTAL_SALE_COUNT'] = $last30d[$id]['TOTAL_SALE_COUNT'];
    $trendByCount[$id]['TOTAL_SALE_COUNT_60D'] = $last60d30d[$id]['TOTAL_SALE_COUNT'];
    $trendByCount[$id]['TOTAL'] = $last30d[$id]['TOTAL'];
    $trendByCount[$id]['GAIN_COUNT_PERC'] = (($last30d[$id]['TOTAL_SALE_COUNT']-$last60d30d[$id]['TOTAL_SALE_COUNT'])/$last60d30d[$id]['TOTAL_SALE_COUNT'])*100;
    $trendByCount[$id]['GAIN_TOT_PERC'] = (($last30d[$id]['TOTAL']-$last60d30d[$id]['TOTAL'])/$last60d30d[$id]['TOTAL'])*100;
    $last60d30dSaleCount = $last60d30d[$id]['TOTAL_SALE_COUNT'];
    $last60d30dTot = $last60d30d[$id]['TOTAL'];
    $trendByCount[$id]['GAIN_COUNT'] = ($last30d[$id]['TOTAL_SALE_COUNT']-$last60d30dSaleCount);
    $trendByCount[$id]['GAIN_TOT'] = ($last30d[$id]['TOTAL']-$last60d30dTot);

    $gain[$id] = $trendByCount[$id]['GAIN_COUNT'];
    $gaintot[$id] = $trendByCount[$id]['GAIN_TOT'];
  } else {
    $trendByCount[$id]['NAME'] = $last60d30d[$id]['NAME'];
    $trendByCount[$id]['CODE'] = $last60d30d[$id]['CODE'];
    $trendByCount[$id]['PRICESELL'] = $last60d30d[$id]['PRICESELL'];
    $trendByCount[$id]['TOTAL_SALE_COUNT'] = 0;
    $trendByCount[$id]['TOTAL_SALE_COUNT_60D'] = $last60d30d[$id]['TOTAL_SALE_COUNT'];
    $trendByCount[$id]['TOTAL'] = 0;

    $trendByCount[$id]['GAIN_COUNT_PERC'] = ((0-$last60d30d[$id]['TOTAL_SALE_COUNT'])/$last60d30d[$id]['TOTAL_SALE_COUNT'])*100;
    $trendByCount[$id]['GAIN_TOT_PERC'] = ((0-$last60d30d[$id]['TOTAL'])/$last60d30d[$id]['TOTAL'])*100;

    $trendByCount[$id]['GAIN_COUNT'] = (0-$last60d30d[$id]['TOTAL_SALE_COUNT']);
    $trendByCount[$id]['GAIN_TOT'] = (0-$last60d30d[$id]['TOTAL']);

    $gain[$id] = $trendByCount[$id]['GAIN_COUNT'];
    $gaintot[$id] = $trendByCount[$id]['GAIN_TOT'];

  }
}

foreach ($last30d as $id => $item) {
  if (isset($gain[$id])) {
    continue;
  }
  if ($last30d[$id]['TOTAL'] < 100 || $last30d[$id]['TOTAL_SALE_COUNT'] < 5) {
    // continue;
  }
  
  $trendByCount[$id]['NAME'] = $last30d[$id]['NAME'];
  $trendByCount[$id]['CODE'] = $last30d[$id]['CODE'];
  $trendByCount[$id]['PRICESELL'] = $last30d[$id]['PRICESELL'];
  $trendByCount[$id]['TOTAL_SALE_COUNT'] = $last30d[$id]['TOTAL_SALE_COUNT'];
  $trendByCount[$id]['TOTAL_SALE_COUNT_60D'] = 0;
  if (isset($last60d30d[$id]['TOTAL_SALE_COUNT'])) {
    die($id); ////////////////////////// NEVER should come to here
  }
  $trendByCount[$id]['TOTAL'] = $last30d[$id]['TOTAL'];
  $trendByCount[$id]['GAIN_COUNT_PERC'] = 'N/A';
  $trendByCount[$id]['GAIN_TOT_PERC'] = 'N/A';
  $last60d30dSaleCount = 0;
  $last60d30dTot = 0;
  $trendByCount[$id]['GAIN_COUNT'] = ($last30d[$id]['TOTAL_SALE_COUNT']-$last60d30dSaleCount);
  $trendByCount[$id]['GAIN_TOT'] = ($last30d[$id]['TOTAL']-$last60d30dTot);

  $gain[$id] = $trendByCount[$id]['GAIN_COUNT'];
  $gaintot[$id] = $trendByCount[$id]['GAIN_TOT'];
  
}

/////////////// gain ////////////

$i = 0; $i50 = 0; $i100 =0;
$inc = 0;
arsort($gain);
$gain1 = $gain; //array_slice($gain, 0, 20);
foreach ($gain1 as $k => $g) {
  if ($trendByCount[$k]['GAIN_COUNT'] <= 0) {
    continue;
  }
  if (round($trendByCount[$k]['GAIN_COUNT_PERC']) >= 50) {
    $i50++;
  }
  if (round($trendByCount[$k]['GAIN_COUNT_PERC']) >= 100) {
    $i100++;
  }
  $i++;
}
echo $i.' Items (='.round(($i/$menuProdCount)*100).'% of total items) showed increase in sale'."\n";
echo $i50.' Items (='.round(($i50/$menuProdCount)*100).'% of total items) showed over 50% increase  in sale'."\n";
echo $i100.' Items (='.round(($i100/$menuProdCount)*100).'% of total items) showed over 100% increase  in sale'."\n";

/////////////// gain ////////////

/////////////// loss ////////////
$inc = 0;
asort($gain);
$gain2 = $gain; //array_slice($gain, 0, 20);
$i = 0; $i25 = 0; $i50 = 0;

foreach ($gain2 as $k => $g) {
  if ($trendByCount[$k]['GAIN_COUNT'] > 0 || $trendByCount[$k]['TOTAL_SALE_COUNT'] < 1) {
    continue;
  }
  if ($trendByCount[$k]['GAIN_COUNT_PERC'] <= -25) {
    $i25++;
  }
  if ($trendByCount[$k]['GAIN_COUNT_PERC'] <= -50) {
    $i50++;
  }

  $i++;
  // if ($inc == 20) {    break; }
}
/////////////// loss ////////////

echo $i.' Items (='.round(($i/$menuProdCount)*100).'% of total items) showed decrease in sale'."\n";
echo $i25.' Items (='.round(($i25/$menuProdCount)*100).'% of total items) showed over 25% decrease  in sale'."\n";
echo $i50.' Items (='.round(($i50/$menuProdCount)*100).'% of total items) showed over 50% decrease  in sale'."\n";

// fclose($fp);
?>
